package club.beenest.framework.oss.config;

import club.beenest.framework.oss.service.IFileStrategyClient;
import club.beenest.framework.oss.service.impl.TencentFileClient;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.region.Region;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@ConditionalOnProperty(value = "bee.oss.enabled", havingValue = "true")
@Configuration
@EnableConfigurationProperties(BeeOssProperties.class)
public class BeeOssAutoConfiguration {

    @ConditionalOnProperty(value = "bee.oss.mode", havingValue = "tx")
    @Bean
    public IFileStrategyClient iFileStrategyClient(BeeOssProperties beeOssProperties) {
        String regionName = beeOssProperties.getRegion();
        COSCredentials cred =
                new BasicCOSCredentials(beeOssProperties.getSecretId(), beeOssProperties.getSecretKey());
        Region region = new Region(regionName);
        ClientConfig clientConfig = new ClientConfig(region);
        clientConfig.setHttpProtocol(HttpProtocol.https);
        COSClient cosClient =  new COSClient(cred, clientConfig);

        return new TencentFileClient(cosClient, beeOssProperties.getBucket());
    }
}
